package jiemian1;

import gonggong.Message;
import gonggong.MessageType;
import gonggong.User;
import gongju.Glhylb;
import gongju.Glxc;
import moxing.clientrec;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Calendar;

public class qunliao {
    public JPanel Jpanel1;
    public  JTextArea messArea;
    private JButton fileBtn;
    private JButton sendBtn;
    private JTextArea sendArea;
    private static JFrame frame;
    private Socket socket=null;
    public Thread recv;
    private static final int BUFSIZE=8096; //缓冲区大小
    private File file; //文件

    public qunliao(){
//       fileBtn=new JButton(new ImageIcon("C:\\Users\\浅汐\\Desktop\\image\\denglu.gif"));
//        messArea.setEditable(false);

        fileBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
        sendBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
    }

    public qunliao(User u){
        super();
//        messArea.setEditable(false);
        //socket=Glxc.getLjfwqxc(u.getUserId()).getS();
//        clientrec rec= null;
//            rec = new clientrec(socket,messArea);//
//            rec.start();

        fileBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //文件选择框
                JFileChooser fileChooser = new JFileChooser();
                Message msg=new Message();
                //获取当前的目录并设置为程序运行的目录
                fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
                if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
                    file = fileChooser.getSelectedFile();
                    //获取当前选择文件的绝对路径
                    String filePath = fileChooser.getSelectedFile().getAbsolutePath();
                    msg.setGetter(u.getUserId());
                    msg.setCon(file.getName());
                    msg.setFile(file);
                    msg.setMesType(MessageType.message_file);
                    messArea.append(msg.getGetter()+"发送一个文件："+msg.getCon()+"\n");
                    try {
                        ObjectOutputStream oos=new ObjectOutputStream
                                (Glxc.getLjfwqxc(u.getUserId()).getS().getOutputStream());
                        oos.writeObject(msg);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }

                }
            }
        });



//        Thread rec= new Thread(new Runnable() {
//            @Override
//            public void run() {
//
//                while(true){
//                    try {
//                        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
//                        Message m = (Message) ois.readObject();
//
//                        //服务器转发的文件信息
//                        if(m.getMesType().equals(MessageType.message_file))
//                        {
//                            System.out.println("服务器转发的文件信息");
//
//                        }
//                        //服务器转发来的群聊消息
//                        else if (m.getMesType().equals(MessageType.message_MSG))
//                        {
//                            System.out.println("服务器转发的群聊消息");
//                        }
//
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    } catch (ClassNotFoundException e) {
//                        e.printStackTrace();
//                    }
//
//                    //JOptionPane.showMessageDialog(null,"23336");
//
//                }
//            }
//        });

        //消息发送
        sendBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Message msg=new Message();
                    String s = sendArea.getText();
                    msg.setCon(s);
                    msg.setGetter(u.getUserId());
                    messArea.append(msg.getGetter()+"说："+msg.getCon()+"\n");
                    ObjectOutputStream os = new ObjectOutputStream
                            (Glxc.getLjfwqxc(u.getUserId()).getS().getOutputStream());
                    msg.setMesType(MessageType.message_MSG);
                    os.writeObject(msg);
                    sendArea.setText("");

//                    ObjectInputStream ois=new ObjectInputStream(Glxc.getLjfwqxc(u.getUserId()).getS().getInputStream());
//                    Message m=(Message)ois.readObject();
//                    System.out.println(m.getCon());
//                    sendArea.append(m.getCon());
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });


    }

//    public void run() {
//
//        while(true){
//            try {
//                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
//                Message m = (Message) ois.readObject();
//                    //服务器转发的文件信息
//                if(m.getMesType().equals(MessageType.message_file))
//                {
//                    System.out.println(m.getSender()+"发了一个文件："+m.getCon());
//
//                }
//                //服务器转发来的群聊消息
//                else if (m.getMesType().equals(MessageType.message_MSG))
//                {
//                    System.out.println(m.getSender()+"说："+m.getCon());
//                }
//
//            } catch (IOException e) {
//                e.printStackTrace();
//            } catch (ClassNotFoundException e) {
//                e.printStackTrace();
//            }
//            //JOptionPane.showMessageDialog(null,"23336");
//        }
//    }

    public void showMessage(Message m)
    {
        String datetime= Calendar.getInstance().getTime().toLocaleString();
        messArea.append(m.getSender()+" 对我说:    "+m.getCon()+"      "+datetime+"\r\n");
    }

    public static void main(String[] args) {
        frame = new JFrame("qunliao");
        frame.setContentPane(new qunliao().Jpanel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setSize(600,600);
        frame.setLocation(700,200);
    }


}
